package com.example.library.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.library.R;
import com.example.library.model.SingleRoom;

import java.util.List;

public class SingleRoomAdapter extends RecyclerView.Adapter<SingleRoomAdapter.RoomViewHolder> {
    private List<SingleRoom> roomList;
    private OnRoomClickListener listener;
    private boolean isAdmin;

    public interface OnRoomClickListener {
        void onRoomClick(SingleRoom room);
    }

    public SingleRoomAdapter(List<SingleRoom> roomList, OnRoomClickListener listener, boolean isAdmin) {
        this.roomList = roomList;
        this.listener = listener;
        this.isAdmin = isAdmin;
    }

    @NonNull
    @Override
    public RoomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_single_room, parent, false);
        return new RoomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RoomViewHolder holder, int position) {
        final SingleRoom room = roomList.get(position);
        holder.tvRoomNumber.setText(room.getRoomNumber());
        holder.tvStatus.setText(room.getStatus());

        // 根据状态设置不同的背景颜色
        int backgroundColor;
        if (room.isClosedForUse()) {
            backgroundColor = R.color.closed;
        } else if (room.isAvailable()) {
            backgroundColor = R.color.available;
        } else {
            backgroundColor = R.color.reserved;
        }

        holder.cardView.setCardBackgroundColor(
                ContextCompat.getColor(holder.itemView.getContext(), backgroundColor));
        holder.tvRoomNumber.setTextColor(
                ContextCompat.getColor(holder.itemView.getContext(), android.R.color.white));
        holder.tvStatus.setTextColor(
                ContextCompat.getColor(holder.itemView.getContext(), android.R.color.white));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isAdmin && room.isClosedForUse()) {
                    // 如果是学生且房间已关闭使用，显示提示对话框
                    showClosedDialog(v.getContext());
                } else if (listener != null) {
                    listener.onRoomClick(room);
                }
            }
        });
    }

    private void showClosedDialog(Context context) {
        new AlertDialog.Builder(context)
                .setTitle("房间不可用")
                .setMessage("该房间暂停使用中，请选择其他房间。")
                .setPositiveButton("确定", null)
                .show();
    }

    @Override
    public int getItemCount() {
        return roomList.size();
    }

    static class RoomViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        TextView tvRoomNumber;
        TextView tvStatus;

        RoomViewHolder(View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.cardView);
            tvRoomNumber = itemView.findViewById(R.id.tvRoomNumber);
            tvStatus = itemView.findViewById(R.id.tvStatus);
        }
    }
} 